package cn.tedu.myweb.controller;


import cn.tedu.myweb.entity.FileList;
import cn.tedu.myweb.mapper.FileListMapper;
import cn.tedu.myweb.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class FileListController {

    @Autowired(required = false)
    FileListMapper mapper;

    @Autowired
    UserMapper userMapper;

    //查私人文件列表
    @RequestMapping("/fileList/selectFileList")
    public List<FileList> selectNoFather(@AuthenticationPrincipal UserDetails user
                                                     ,HttpSession session){
        return mapper.selectFileListByDid(userMapper.findUserByUsername(user.getUsername()).getId());
    }

    //查公共文件列表
    @RequestMapping("/fileList/selectPubFileList")
    public List<FileList> selectPubFileList(@AuthenticationPrincipal UserDetails user
                                            ,HttpSession session){

        /**
         * user以后用来实现"仅好友"权限,可在公共网盘看到该权限且为好友的文件
         */

        return mapper.selectPubFileList(userMapper.findUserByUsername(user.getUsername()));
    }

    //下载 只增加数据库中下载次数,下载操作由浏览器完成
    @RequestMapping("/fileList/download")
    public void download(Integer fid){
        mapper.download(fid);
    }

}
